﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using Core;
using Models;

namespace Controllers
{
    public class BaseController:Controller
    {
        protected PageViewModel Pagemodel=new PageViewModel();
        protected infoEntity info;
        protected string suffix = string.Empty;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var infoCore = new infoCore();
            info = infoCore.GetOne(1);
            if (info == null)
            {
                throw new Exception("站点信息不完善！");
            }
            infoEntity mobileinfo;
            if (Request.Url != null)
            {
                //如果有id>0能匹配到weburl的地址获取对应信息站点
                mobileinfo = infoCore.GetOneByWebUrl(Request.Url.Host);
            }
            else
            {
                if(info.mweburl.ToLower()==Request.Url.Host.ToLower())
                {
                    mobileinfo = info;
                }
                else
                {
                    mobileinfo = new infoEntity();
                }
            }

            if (Request.Browser.IsMobileDevice||mobileinfo.ID>0)
            {
                if (mobileinfo.ID > 0)
                {
                    suffix = ".mobile";
                    info = mobileinfo;
                }
                Pagemodel.skin = string.IsNullOrEmpty(info.mskin) ? "/skin" : "/skin/" + info.mskin;
                Pagemodel.pagetitle = info.pre_title + info.mpage_title + info.end_title;
                Pagemodel.keywords = info.mpage_key;
                Pagemodel.description = info.mpage_desc;
                Pagemodel.copyright = info.mcopyright;
                Pagemodel.content = info.mbody;
            }
            else
            {
                Pagemodel.skin = string.IsNullOrEmpty(info.skin) ? "/skin" : "/skin/" + info.skin;
                Pagemodel.pagetitle = info.pre_title + info.page_title + info.end_title;
                Pagemodel.keywords = info.page_key;
                Pagemodel.description = info.page_desc;
                Pagemodel.copyright = info.copyright;
                Pagemodel.content = info.body;
            }
            Pagemodel.sitename = info.name;
            Pagemodel.cur = @"<a href=""/"" class=""home"">首页</a>";
            Pagemodel.info=new Dictionary<string, object>();
            Pagemodel.info.Add("info",info);
            Pagemodel.cbanner = info.bannerpic;
            ViewBag.model = Pagemodel;
            base.OnActionExecuting(filterContext);
        }

    }
}

